 

import UIKit
import SwiftUI
import Flutter
 
import ZegoExpressEngine
class LivePreviewView:NSObject, FlutterPlatformView{
    
    let previewView = ViewWrapper()
    var roomMais:LSRoommaisModel
    lazy var nameLabel: UIHostingController = {
        
        liveMessagePlugin?.previewView = previewView.view
        return UIHostingController(rootView: LivePreView(previewView: previewView))
    }()
    
    
    
    init(_ frame: CGRect,viewID: Int64,args :Any?,messenger :FlutterBinaryMessenger) {
        
        if let args = args as? [String: Any], let roomMaisJson = args["roomMais"] as? String {
            self.roomMais = parseJSON(jsonString: roomMaisJson, to: LSRoommaisModel.self)!
        }else{
            self.roomMais = LSRoommaisModel();
        }
        
        super.init()
        liveMessagePlugin?.livePreviewMap[self.roomMais.maiIndex] = self
        
    }
    
    
    
    required init?(coder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
        
    }
    
    
    
    func view() -> UIView {
        
        return nameLabel.view
        
    }
     
    func startPreview(){
        let previewView = self.previewView.view
        var previewCanvas = ZegoCanvas()
        previewCanvas.view = previewView
        previewCanvas.viewMode = ZegoViewMode.aspectFit
        publishSetting()
        
        
        if  roomMais.isAnchor() && roomMais.status != 0{
            ZegoExpressEngine.shared().muteMicrophone(roomMais.maiOpen==0)
            ZegoExpressEngine.shared().startPreview(previewCanvas)
            ZegoExpressEngine.shared().startPublishingStream(roomMais.inputStreamId)
      
        }else{
            ZegoExpressEngine.shared().startPlayingStream(roomMais.outputStreamId , canvas: previewCanvas)
        }
        
    }
    
    
    func publishSetting(){
        ZegoExpressEngine.shared().enableCamera(true)
        //            ZegoExpressEngine.shared().muteSpeaker(false)
        //            ZegoExpressEngine.shared().enableHardwareEncoder(false)

        
        var videoConfig = ZegoVideoConfig(preset: .preset540P)
        var videoMirrorMode = ZegoVideoMirrorMode.onlyPreviewMirror
        ZegoExpressEngine.shared().setVideoMirrorMode(videoMirrorMode)
        
        var liumav : LSLiumavo3Model?
        if liveMessagePlugin?.roomInfo?.roomMais?.count == 3 {
            liumav = liveMessagePlugin?.appConfig?.liuMaVO3
        }else if liveMessagePlugin?.roomInfo?.roomMais?.count == 7 {
            liumav = liveMessagePlugin?.appConfig?.liuMaVO7
        }else if liveMessagePlugin?.roomInfo?.roomMais?.count == 9 {
            liumav = liveMessagePlugin?.appConfig?.liuMaVO9
        }
        if let liumav = liumav {
            videoConfig.fps = Int32(liumav.frameRate ?? "15") ?? 15
            videoConfig.bitrate =  Int32(liumav.codeRate ?? "400") ?? 400
            videoConfig.encodeResolution.width =  Double(liumav.width ?? "180") ?? 180
            videoConfig.captureResolution.width =  Double(liumav.width ?? "180") ?? 180
            videoConfig.encodeResolution.height =  Double(liumav.length ?? "180") ?? 180
            videoConfig.captureResolution.height =  Double(liumav.length ?? "180") ?? 180
        }
        

        ZegoExpressEngine.shared().setVideoConfig(videoConfig)
        
    }
    
    func PublishingStream(){
        
    }
    
    func PlayingStream(){
        
    }
}

struct LivePreView : View {
    let previewView: ViewWrapper
    init(previewView: ViewWrapper) {
        self.previewView = previewView
    }
    var body: some View {
        ZStack{
            previewView
        }
    }
}

 

class LivePreviewViewFactory:NSObject, FlutterPlatformViewFactory {

    var messenger: FlutterBinaryMessenger

    init(messenger:FlutterBinaryMessenger) {
          self.messenger = messenger
          super.init()

      }


    func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {

           return LivePreviewView(frame,viewID: viewId,args: args,messenger: messenger)

       }


       func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {

           return FlutterStandardMessageCodec.sharedInstance()

       }

      

}


#if canImport(UIKit)

struct ViewWrapper: UIViewRepresentable {
    let view = UIView()

    func makeUIView(context: Context) -> some UIView {
        return view
    }
 
    func updateUIView(_ uiView: UIViewType, context: Context) {
    }

}

#elseif os(OSX)

struct ViewWrapper: NSViewRepresentable {

    let view = NSView()

    func makeNSView(context: Context) -> some NSView {
        return view
    }

    func updateNSView(_ nsView: NSViewType, context: Context) {
    }
}

#endif
